package com.teamwin.hdmi.donate;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class Installer extends SocketManager {
	private int daemonVersion;
	private int installReason;
	
	class ButtonListener implements View.OnClickListener {
		Installer parent;
		
		public ButtonListener(Installer parent) {
			this.parent = parent;
		}

		@Override
		public void onClick(View v) {
			InstallerThread installer = null;
			switch (installReason) {
				case SocketManager.NEW_DAEMON:
					installer = new InstallerThread(this.parent, SocketManager.NEW_DAEMON);
					installer.start();
					break;
				case SocketManager.NEED_KERNEL:
					startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://tinyw.in/Sl")));
//					installer = new InstallerThread(this.parent, SocketManager.NEED_KERNEL);
					break;
				default:
					break;
			}
			
			Button b = (Button) findViewById(R.id.install);
			b.setEnabled(false);

			ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar1);
			progressBar.setIndeterminate(true);
			progressBar.setVisibility(View.VISIBLE);
			
			TextView tv2 = (TextView)findViewById(R.id.textView1);
			tv2.setText("Installing... please wait!");
			tv2.setVisibility(View.VISIBLE);

//			findViewById(R.id.textView1).setVisibility(View.VISIBLE);
//			b.setEnabled(false);
//
//			InstallerThread installer = new InstallerThread(this.parent);
//			installer.start();
		}
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.install);
		
		Button b = (Button)findViewById(R.id.install);
		b.setOnClickListener(new ButtonListener(this));
		
		installReason = getIntent().getIntExtra("installer_reason", 0);
		System.out.println("installer reason: " + installReason);
		switch (installReason) {
			case SocketManager.NEW_DAEMON:
				this.setDaemonVersion(getIntent().getIntExtra("latest_version", 0));
				System.out.println("daemon version: " + this.getDaemonVersion());
				b.setText("Start Download");
				TextView tv = (TextView)findViewById(R.id.labelInstallDescription);
				tv.setText("New module available! Click to download.");
				
				break;
			case SocketManager.NEED_KERNEL:
				b.setText("Open Webpage");
			default:
				break;
		}
	}

	public int getDaemonVersion() {
		return daemonVersion;
	}

	public void setDaemonVersion(int daemonVersion) {
		this.daemonVersion = daemonVersion;
	}
}
